#!/bin/bash

if [ -d .git ]; then
    echo ".git already exist, only work in new directory"
    exit 1
fi

if [ $(pwd) = $HOME ]; then
    read -r -p "you are now at your home directory, confirm? [y/N]" input
    case ${input} in 
        y | Yes | YES | Y )
	    echo "you said: ${input} ..."
            ;;
        *)
	    echo "abort !!"
            exit 0
    esac
fi

git init

cat << EOF > .gitignore
# Default git init c++ template by wbvalid

# vscode default dir
.vscode/

# c++ artifactory
*.out
*.exe
*.d
*.log
*.o

# cmake default work directory
build/

EOF

cat << EOF >> build.sh

#!/bin/bash

if [ -n `which ninja 2>/dev/null` ];then

cmake -H. -Bbuild -G Ninja -DCMAKE_BUILD_TYPE=Debug
ninja -C build
else
cmake -H. -Bbuild -DCMAKE_BUILD_TYPE=Debug
make -Cj build
fi


EOF

chmod +x build.sh

cat << EOF >> example.cpp
#include <cstdio>
void foo()
{
    printf("hello world\n");
}

EOF

cat << EOF >> CMakeLists.txt
# generated example CMakeLists.txt

cmake_minimum_required(VERSION 3.16)

# replace project name below
project("example" LANGUAGES CXX VERSION 1.0.0)

set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_EXTENSIONS OFF)

message("CMAKE build for \${PROJECT_NAME}_V\${PROJECT_VERSION}")

# write your code here
# replace the add_library below

add_library(\${PROJECT_NAME} STATIC example.cpp)

# install settings

install(TARGETS \${PROJECT_NAME}
    RUNTIME DESTINATION "bin"
    ARCHIVE DESTINATION "lib"
    LIBRARY DESTINATION "lib"
)

# package settings
# replace detail information below
set(CPACK_PACKAGE_DIRECTORY \${PROJECT_SOURCE_DIR}/pack)
set(CPACK_PACKAGE_NAME \${PROJECT_NAME})
set(CPACK_PACKAGE_VERSION \${PROJECT_VERSION})
set(CPACK_PACKAGING_INSTALL_PREFIX \${PROJECT_NAME})
set(CPACK_PACKAGE_GROUP "cmschina")
set(CPACK_PACKAGE_CONTACT "wangbo20@cmschina.com.cn")
set(CPACK_PACKAGE_VENDOR "cmschina")
set(CPACK_GENERATOR "TGZ")
include(CPack)

EOF


git add .
git commit -m "Default Cxx Template Init"

